package cn.iocoder.yudao.module.wms.controller.admin.sale;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.sale.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.sale.SaleDO;
import cn.iocoder.yudao.module.wms.service.sale.SaleService;

@Tag(name = "管理后台 - 销售")
@RestController
@RequestMapping("/wms/sale")
@Validated
public class SaleController {

    @Resource
    private SaleService saleService;

    @PostMapping("/create")
    @Operation(summary = "创建销售/销售开票")
    @PreAuthorize("@ss.hasPermission('wms:sale:create')")
    public CommonResult<String> createSale(@Valid @RequestBody SaleSaveReqVO reqVO) {
        return success(saleService.createSale(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新销售")
    @PreAuthorize("@ss.hasPermission('wms:sale:update')")
    public CommonResult<Boolean> updateSale(@Valid @RequestBody SaleSaveReqVO updateReqVO) {
        saleService.updateSale(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除销售")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:sale:delete')")
    public CommonResult<Boolean> deleteSale(@RequestParam("id") String id) {
        saleService.deleteSale(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得销售")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:sale:query')")
    public CommonResult<SaleRespVO> getSale(@RequestParam("id") String id) {
        SaleDO sale = saleService.getSale(id);
        return success(BeanUtils.toBean(sale, SaleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得销售分页")
    @PreAuthorize("@ss.hasPermission('wms:sale:query')")
    public CommonResult<PageResult<SaleRespVO>> getSalePage(@Valid SalePageReqVO pageReqVO) {
        PageResult<SaleDO> pageResult = saleService.getSalePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SaleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出销售 Excel")
    @PreAuthorize("@ss.hasPermission('wms:sale:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSaleExcel(@Valid SalePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SaleDO> list = saleService.getSalePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售.xls", "数据", SaleRespVO.class,
                        BeanUtils.toBean(list, SaleRespVO.class));
    }

    @PutMapping("/settle/{id}")
    @Operation(summary = "结款")
    @PreAuthorize("@ss.hasPermission('wms:sale:settle')")
    public CommonResult<Boolean> settleSale(@PathVariable String id) {
        saleService.settleSale(id);
        return success(true);
    }

}